package cn.zzdt4j.config.model;

import cn.zzdt4j.common.constant.Constants;
import cn.zzdt4j.common.toolkit.Md5Util;
import cn.zzdt4j.config.toolkit.SimpleReadWriteLock;
import cn.zzdt4j.config.toolkit.SingletonRepository;
import lombok.Data;

/**
 * The type Cache item.
 *
 * @author by <a href="mailto:ligang941012@gmail.com">gang.Li</a>
 * @since 2023 /11/8 15:18
 */
@Data
public class CacheItem {

    final String groupKey;

    private volatile String md5 = Constants.NULL;

    private volatile long lastModifiedTs;

    private volatile ConfigAllInfo configAllInfo;

    private SimpleReadWriteLock rwLock = new SimpleReadWriteLock();

    public CacheItem(String groupKey) {
        this.groupKey = SingletonRepository.DataIdGroupIdCache.getSingleton(groupKey);
    }

    public CacheItem(String groupKey, String md5) {
        this.md5 = md5;
        this.groupKey = SingletonRepository.DataIdGroupIdCache.getSingleton(groupKey);
    }

    public CacheItem(String groupKey, ConfigAllInfo configAllInfo) {
        this.configAllInfo = configAllInfo;
        this.md5 = Md5Util.getTpContentMd5(configAllInfo);
        this.groupKey = SingletonRepository.DataIdGroupIdCache.getSingleton(groupKey);
    }
}
